/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.demo.scheduler.service;

import info.peigen.hotpot.demo.scheduler.domain.Stub;
import info.peigen.hotpot.demo.scheduler.domain.Task;
import info.peigen.hotpot.demo.scheduler.domain.Trade;
import info.peigen.hotpot.demo.scheduler.service.communication.MqttMessage;
import info.peigen.hotpot.demo.scheduler.service.communication.MqttMock;
import info.peigen.hotpot.demo.scheduler.service.communication.SocketMessage;
import info.peigen.hotpot.demo.scheduler.service.communication.SocketMock;
import info.peigen.hotpot.demo.scheduler.service.step.base.TaskContext;
import info.peigen.hotpot.demo.scheduler.service.task.TaskService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.Solon;

import java.util.function.Consumer;

/**
 * <b>(Demo)</b>
 *
 * @author Lihai
 * @version 1.0.0
 * @since 2023/9/9
 */
@Slf4j
public class Demo {
    @SneakyThrows
    public static void main(String[] args) {
        Solon.start(Demo.class, args, app -> Solon.cfg());

        String tradeId      = "trade01";
        String stationId    = "station01";
        String parkingSpace = "A-442";
        Trade  trade        = new Trade().setTradeId(tradeId).setStationId(stationId).setParkingSpace(parkingSpace).setStub(new Stub().setStubId("stub01").setLocation("A-440"));

        // 下单成功后，创建任务。
        TaskService taskService = Solon.context().getBean(TaskService.class);
        taskService.initTask(trade);

        // 启动任务
        Task task = taskService.getTaskNameMap().get("送电桩");
        taskService.execute(task);

        // autoChargeService.add(sendStubProcessor, chargeReadyProcessor, robotBackProcessor)
        // sendStubProcessor.add(triggerEvent selectStep moveStep upStep moveStep downStep doneEvent)
        // chargeReadyProcessor.add(triggerEvent,stubWakeUpStep)
        // robotBackProcessor.add(triggerEvent,robotBackStep)

        // 模拟回执
        log.info("==============================================");
        log.info("任务-{},状态:{}", task.getName(), task.getStatus());
        log.info("==============================================");
        Consumer<TaskContext> consumer = task.getCurrentStep()::handleCallback;
        MqttMock              mqttMock = new MqttMock();
        mqttMock.receiveMessage(MqttMessage.builder().requestNo(task.getTaskId() + "-calc").robotId("robot01").message("a-b，距离100米，预估用时1分钟").build(), consumer);
        mqttMock.receiveMessage(MqttMessage.builder().requestNo(task.getTaskId() + "-move").robotId("robot01").message("移动完成").build(), consumer);
        mqttMock.receiveMessage(MqttMessage.builder().requestNo(task.getTaskId() + "-giveUp").robotId("robot01").message("托举完成").build(), consumer);
        mqttMock.receiveMessage(MqttMessage.builder().requestNo(task.getTaskId() + "-move").robotId("robot01").message("移动完成").build(), consumer);
        mqttMock.receiveMessage(MqttMessage.builder().requestNo(task.getTaskId() + "-putDown").robotId("robot01").message("放下完成").build(), consumer);
        Thread.sleep(200);
        log.info("==============================================");
        log.info("任务-{},状态:{}", task.getName(), task.getStatus());
        log.info("==============================================");


        Thread.sleep(200);
        SocketMock socketMock = new SocketMock();
        Task       task2      = taskService.getTaskNameMap().get("充电前准备");
        log.info("==============================================");
        log.info("任务-{},状态:{}", task2.getName(), task2.getStatus());
        log.info("==============================================");
        socketMock.receiveMessage(SocketMessage.builder().requestNo(task2.getTaskId() + "-wakeUp").stubId("stub01").message("电桩唤醒完成").build(), consumer);
        socketMock.receiveMessage(SocketMessage.builder().requestNo(task2.getTaskId() + "-armLeft").stubId("stub01").message("臂左移完成").build(), consumer);
        socketMock.receiveMessage(SocketMessage.builder().requestNo(task2.getTaskId() + "-armUp").stubId("stub01").message("臂上摆完成").build(), consumer);
        socketMock.receiveMessage(SocketMessage.builder().requestNo(task2.getTaskId() + "-cableOut").stubId("stub01").message("线伸出完成").build(), consumer);
        log.info("==============================================");
        log.info("任务-{},状态:{}", task2.getName(), task2.getStatus());
        log.info("==============================================");

        Thread.sleep(200);
        // mock mqtt message
        Task task3 = taskService.getTaskNameMap().get("机器人返回");
        log.info("==============================================");
        log.info("任务-{},状态:{}", task3.getName(), task3.getStatus());
        log.info("==============================================");
        mqttMock.receiveMessage(MqttMessage.builder().requestNo(task3.getTaskId() + "-move").robotId("robot01").message("移动完成").build(), consumer);
        log.info("==============================================");
        log.info("任务-{},状态:{}", task3.getName(), task3.getStatus());
        log.info("==============================================");


    }
}
